<template>
  <div class="app-container">
    <el-card class="query-card">
      <el-form ref="query" :model="page.query" inline label-position="right" label-width="100px">
        <el-form-item label="外发日期" prop="dateRange">
          <el-date-picker
            v-model="page.query.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="外发编号" prop="osOrderNo">
          <el-input v-model="page.query.osOrderNo" placeholder="请输入外发订单编号" clearable />
        </el-form-item>
        <el-form-item label="原订单编号" prop="orderNo">
          <el-input v-model="page.query.orderNo" placeholder="请输入原订单编号" clearable />
        </el-form-item>
<!--        <el-form-item label="客户" prop="customerId">
          <customer-query ref="customerQuery" v-model="page.query.customerId" @choose="handleChooseCustomer" />
        </el-form-item>-->
        <el-form-item label="供应商" prop="supplierId">
          <supplier-query ref="supplierQuery" v-model="page.query.supplierId" @choose="handleChooseSupplier" />
        </el-form-item>
<!--        <el-form-item label="外发订单状态" prop="osOrderState">
          <dict-select
            ref="dictSelect1"
            v-model="page.query.osOrderState"
            type="button"
            dict-key="os_order_state"
            placeholder="请选择外发订单状态"
            @input="handleDictSelectChange"
          />
        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQueryForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
      <el-tabs v-model="page.query.osOrderState" @tab-click="handleTabClick">
        <el-tab-pane
          v-for="(item,index) in osOrderStateList"
          :key="index"
          :label="item.stateName+'('+item.count+')'"
          :name="item.state"
        />
      </el-tabs>
      <!--      <div>
        <span>销售订单列表</span>
      </div>
      <el-divider />-->
      <div class="table-button">
        <el-button
          type="primary"
          :loading="tableLoading"
          class="el-icon-plus"
          @click="saveClick"
        >新增外发订单
        </el-button>
        <el-button
          type="warning"
          plain
          :loading="tableLoading"
          class="el-icon-edit"
          @click="updateClick('tableBtn')"
        >修改订单
        </el-button>
<!--        <el-button
          type="info"
          plain
          :loading="tableLoading"
          class="el-icon-warning"
          @click="cancelClick('tableBtn')"
        >取消外发订单
        </el-button>-->
        <el-button type="danger" :loading="tableLoading" plain class="el-icon-folder-delete" @click="handleBatchDelete">批量删除</el-button>
<!--        <el-button type="success" :loading="tableLoading" plain class="el-icon-check" @click="handleBatchComplete">完成订单</el-button>-->
        <el-tooltip content="刷新" class="table-right-btn">
          <el-button size="mini" circle icon="el-icon-refresh" @click="doPage" />
        </el-tooltip>
      </div>
      <el-table
        v-loading="tableLoading"
        :data="tableData"
        style="width: 100%"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="45" />
        <el-table-column label="外发编号" prop="osOrderNo" min-width="110" />
        <el-table-column label="订单编号" prop="orderNo" min-width="110">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="infoClick(scope.row)"
            >{{ scope.row.orderNo }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="外发日期" prop="date" min-width="100" />
        <el-table-column label="外发供应商" prop="supplierName" min-width="100" />
        <el-table-column label="产品名称" prop="productName" min-width="200" />
        <el-table-column label="订单单价" prop="orderUnitPrice" min-width="80">
          <template slot-scope="scope">
            <money-format v-model="scope.row.orderUnitPrice" />
          </template>
        </el-table-column>
        <el-table-column label="外发单价" prop="unitPrice" min-width="80">
          <template slot-scope="scope">
            <money-format v-model="scope.row.unitPrice" />
          </template>
        </el-table-column>
        <el-table-column label="外发数量" prop="quantity" min-width="90">
          <template slot-scope="scope">
            {{ scope.row.quantity }}{{ scope.row.unit }}
          </template>
        </el-table-column>
        <el-table-column label="总金额" prop="amount" min-width="110">
          <template slot-scope="scope">
            <money-format v-model="scope.row.amount" />
          </template>
        </el-table-column>
        <el-table-column label="利润" prop="profit" min-width="110">
          <template slot-scope="scope">
            <money-format v-model="scope.row.profit" />
          </template>
        </el-table-column>
        <el-table-column label="备注" prop="remark" />
<!--        <el-table-column label="外发状态" prop="osOrderState" min-width="120">
          <template slot-scope="scope">
            <os-order-state-tag v-model="scope.row.osOrderState" />
          </template>
        </el-table-column>-->
        <el-table-column label="操作" min-width="60" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="updateClick('column',scope.row)"
            >修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-sizes="[10, 20, 30, 40]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <div v-if="dialogVisible">
      <submit-module ref="submitModule" :visible="dialogVisible" @close="handleCloseSubmit" />
    </div>
  </div>
</template>

<script>
import {
  os_order_delete,
  os_order_page,
  os_order_cancel,
  os_order_state_count,
  os_order_complete
} from '@/api/sale/osOrder'
import submitModule from '@/views/sale/osOrder/modules/submitModule.vue'

export default {
  name: 'Index',
  components: { submitModule },
  data() {
    return {
      tableData: [],
      tableLoading: false,
      selected: [],
      dialogVisible: false,
      osOrderStateList: [],
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          orderNo: '',
          osOrderNo: '',
          dateRange: [],
          customerId: '',
          supplierId: '',
          productId: '',
          osOrderState: '-1'
        }
      }
    }
  },
  mounted() {
    this.doPage()
  },
  methods: {
    handleSearch() {
      this.page.page = 1
      this.doPage()
    },
    doPage() {
      // 分页查询
      this.tableLoading = true
      os_order_page(this.page).then(resp => {
        if (resp.success) {
          this.tableData = resp.data.records
          this.page.total = resp.data.total
        }
      }).finally(() => {
        this.tableLoading = false
      })
      // this.selectOsOrderStateCount()
    },
    selectOsOrderStateCount() {
      this.tableLoading = true
      os_order_state_count(this.page.query).then(resp => {
        if (resp.success) {
          this.osOrderStateList = resp.data
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 选中事件
    handleSelectionChange(row) {
      this.selected = row
    },
    resetQueryForm() {
      this.$refs.query.resetFields()
      this.page.query.customerId = undefined
      // this.$refs.customerQuery.resetQueryForm()
      this.$refs.supplierQuery.resetQueryForm()
      // this.$refs.dictSelect1.resetActive()
      this.page.query.osOrderState = '-1'
      this.doPage()
    },
    // 新增按钮点击
    saveClick() {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init(null, null)
      })
    },
    // 修改按钮点击
    updateClick(type, row) {
      if (type === 'tableBtn') {
        if (this.selected.length === 0) {
          this.$message.warning('请选择一条订单')
          return
        } else if (this.selected.length !== 1) {
          this.$message.warning('只能选择一条订单')
          return
        }
        this.dialogVisible = true
        this.$nextTick(() => {
          this.$refs.submitModule.init(this.selected[0].id, this.selected[0].orderId)
        })
      } else {
        this.dialogVisible = true
        this.$nextTick(() => {
          this.$refs.submitModule.init(row.id, row.orderId)
        })
      }
    },
    // 取消订单
    cancelClick(type, row) {
      let data
      if (type === 'tableBtn') {
        if (this.selected.length === 0) {
          this.$message.warning('请选择一条订单')
          return
        } else if (this.selected.length !== 1) {
          this.$message.warning('只能选择一条订单')
          return
        }
        data = this.selected[0]
      } else {
        data = row
      }
      this.$confirm('是否取消此外发订单，取消后原订单状态将会变成待处理', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tableLoading = true
        os_order_cancel(data.id).then(resp => {
          if (resp.success) {
            this.$message.success('取消成功')
            this.doPage()
          }
        }).finally(() => {
          this.tableLoading = false
        })
      })
    },
    // 详情按钮点击
    infoClick(row) {
      this.$router.push({ path: '/order/saleOrder/detail', query: { id: row.orderId, from: this.$route.path }})
    },
    // 批量删除
    handleBatchDelete() {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条订单')
        return
      }
      this.$confirm('是否批量删除选中的订单?', '删除提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.tableLoading = true
        const data = { ids: this.selected.map(item => item.id) }
        os_order_delete(data).then(resp => {
          if (resp.success) {
            this.$message.success('删除成功')
          }
        }).finally(() => {
          this.doPage()
        })
      })
    },
    // 完成订单
    handleBatchComplete() {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条订单')
        return
      }
      this.$confirm(`是将选中的${this.selected.length}条外发订单修改为已完成?`, '外发订单完成提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tableLoading = true
        const data = { ids: this.selected.map(item => item.id) }
        os_order_complete(data).then(resp => {
          if (resp.success) {
            this.$message.success('外发订单已完成')
          }
        }).finally(() => {
          this.doPage()
        })
      })
    },
    // 分页大小
    handleSizeChange(val) {
      this.page.size = val
      this.doPage()
    },
    // 页码
    handleCurrentChange(val) {
      this.page.page = val
      this.doPage()
    },
    handleDelConfirm(row) {
      this.tableLoading = true
      const data = { ids: [row.id] }
      os_order_delete(data).then(resp => {
        if (resp.success) {
          this.$message.success('删除成功')
          this.doPage()
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    handleChooseCustomer(row) {
      if (row) {
        this.page.query.customerId = row.id
      }
    },
    handleChooseSupplier(row) {
      if (row) {
        this.page.query.supplierId = row.id
      }
    },
    handleCloseSubmit() {
      this.dialogVisible = false
      this.doPage()
    },
    handleTabClick(tab) {
      this.page.query.osOrderState = tab.name
      this.doPage()
    },
    handleDictSelectChange() {
      this.doPage()
    }
  }
}
</script>

<style scoped>

</style>
